package rivers.yeah.dao;

import java.sql.Connection; 
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class BaseDao {
	
	private Connection mConn;
    private PreparedStatement mPreStmt;
    
    public BaseDao() {
    	this.mConn = null;
    	this.mPreStmt = null;
    	
    	try 
	 	{
    		Class.forName(DBConst.DRIVER_NAME);
	 	} 
    	catch (ClassNotFoundException e)
    	{
    		e.printStackTrace();
    	}
    }

    public Connection getConn() {
    	if(null == this.mConn)
    	{
	    	try 
		 	{
	    		this.mConn = DriverManager.getConnection(DBConst.CONNET_URL, DBConst.USERNAME, DBConst.PASSWORD);
		 	} 
		 	catch (SQLException e)
		 	{
		 		e.printStackTrace();
		 	}
    	}
        return this.mConn;
    }
    
    public PreparedStatement getPrepStmt(String aSql, boolean aAutoGeneratedKeys)
    {
    	this.mPreStmt = null;
    	if(null!=aSql && !aSql.equals(""))
    	{
	    	try 
	    	{
	    		if(!aAutoGeneratedKeys)
	    		{
	    			this.mPreStmt = this.getConn().prepareStatement(aSql);
	    		}
	    		else
	    		{
	    			this.mPreStmt = this.getConn().prepareStatement(aSql, 
										Statement.RETURN_GENERATED_KEYS);
	    		}
			} 
	    	catch (SQLException e) 
	    	{
				e.printStackTrace();
			}
    	}
    	return this.mPreStmt;
    }
    
}
